package com.zjdiepu.www.manage.api.file;

import java.util.Arrays;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qiniu.common.QiniuException;
import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.ibiz.file.IQiniuFileService;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 项目名称：dpqc-parent-manager-file <br/> 
 * 类名称：QiniuFileController <br/>
 * 类描述： 七牛文件处理<br/>
 * 创建人：winby <br/>
 * 创建时间：2017年4月26日 下午12:23:37  <br/>
 * @version V1.0   <br/>
 */
@RestController
@RequestMapping(value = "QiniuFileController", produces = "application/json;charset=UTF-8")
public class QiniuFileController extends BaseController {
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private IQiniuFileService iQiniuFileService;
	
	/**
	 * @author winby <br/>
	 * @Title: getToken  <br/>
	 * @Description: 获取token <br/>
	 * @param
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "getToken", method = {RequestMethod.POST} )
	public ApiResponse getToken(@RequestParam(defaultValue="png") String fileType, @RequestParam(defaultValue="img")String type) {
		return ApiResponse.success(iQiniuFileService.getToken(fileType, type));
	}

	/**
	 * @author winby <br/>
	 * @Title: encodeVideo  <br/>
	 * @Description: 视频转码 <br/>
	 * @param
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "encodeMedia", method = {RequestMethod.POST} )
	public ApiResponse encodeMedia(String fileName, String mediaType, String attachmentId) throws QiniuException {
		if(!org.apache.commons.lang3.StringUtils.isNoneBlank(fileName, mediaType, attachmentId)){
			return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
		}
		iQiniuFileService.encodeMedia(fileName, mediaType,attachmentId);
		return ApiResponse.success(true);
	}

	/**
	 * @author winby <br/>
	 * @Title: encodeVideo  <br/>
	 * @Description: 删除文件 <br/>
	 * @param fileName
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "removeFile", method = {RequestMethod.POST} )
	public ApiResponse removeFile(String fileName) {
		iQiniuFileService.removeFile(fileName);
		return ApiResponse.success(true);
	}
	/**
	 * @author winby <br/>
	 * @Title: getURL  <br/>
	 * @Description: 获取文件路径 <br/>
	 * @param key
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "getURL", method = {RequestMethod.POST} )
	public ApiResponse getURL(String key, @RequestParam(defaultValue="0") boolean bigImage) throws QiniuException {
		Map<String, String> urls = CollectionInitUtil.emptyMap();
		if(StringUtils.isBlank(key)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
		String url = iQiniuFileService.getURL(key + SystemConsts.THUMBNAIL_SUBBFIX);
		urls.put("url", url);
		if(bigImage) {
			String bigUrl = iQiniuFileService.getURL(key );
			urls.put("bigUrl", bigUrl);
		}
		return ApiResponse.success(urls);
	}


	/**
	 * @param keys
	 * @return
	 * @author winby <br/>
	 * @Title: getURLs  <br/>
	 * @Description: 获取多个文件路径 <br/>
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "getURLs", method = {RequestMethod.POST})
	public ApiResponse getURLs(String keys, @RequestParam(defaultValue="0") boolean bigImage) throws QiniuException {
		Map<String, String> urlsMap = CollectionInitUtil.emptyMap();
		if (StringUtils.isBlank(keys)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
		String[] key = StringUtils.split(keys, ",");
		if(!bigImage){
			Arrays.stream(key).forEach(k -> urlsMap.put(k, iQiniuFileService.getURL(k + SystemConsts.THUMBNAIL_SUBBFIX)));
		}else {
			Arrays.stream(key).forEach(k -> urlsMap.put(k, iQiniuFileService.getURL(k + SystemConsts.THUMBNAIL_SUBBFIX) + "," + iQiniuFileService.getURL(k)));
		}
		return ApiResponse.success(urlsMap);
	}
}
